﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using BalanceManagerApp.DataAccess;

namespace BalanceManagerApp.ViewModel
{
    /// <summary>
    /// The ViewModel for the application's main window.
    /// </summary>
    public class MainWindowViewModel : WorkspaceViewModel
    {
        #region Fields
                
        ReadOnlyCollection<CommandViewModel> _commands;
        WorkspaceViewModel _currentWorkspace;

        #endregion // Fields

        #region Constructor

        public MainWindowViewModel()
        {
            base.DisplayName = "Финансовый учет";
        }

        #endregion // Constructor

        #region Commands

        /// <summary>
        /// Returns a read-only list of commands 
        /// that the UI can display and execute.
        /// </summary>
        public ReadOnlyCollection<CommandViewModel> Commands
        {
            get
            {
                if (_commands == null)
                {
                    List<CommandViewModel> cmds = this.CreateCommands();
                    _commands = new ReadOnlyCollection<CommandViewModel>(cmds);
                }
                return _commands;
            }
        }

        List<CommandViewModel> CreateCommands()
        {
            return new List<CommandViewModel>
            {
               CreateHomeFunc(),
               CreateFlowFundsFunc(),
               CreateReportFunc(),
               CreateItemsFunc(),
               CreateSettingsFunc()
            };
        }

        #endregion // Commands

        #region Workspaces

        public WorkspaceViewModel CurrentWorkspace
        {
            get
            {
                return _currentWorkspace;
            }
            set
            {
                if (_currentWorkspace == value)
                    return;

                if (_currentWorkspace != null)
                    _currentWorkspace.Dispose();
 
                _currentWorkspace = value;

                OnPropertyChanged("CurrentWorkspace");
            }
        }

        #endregion // Workspaces

        #region Private Helpers

        CommandViewModel CreateHomeFunc()
        {
            var command = new RelayCommand(
                o => this.CurrentWorkspace = new HomePageViewModel(),
                o => true);

            var commandViewModel = new CommandViewModel("Главная страница", "Resources/home.png", command);

            return commandViewModel;
        }

        CommandViewModel CreateFlowFundsFunc()
        {
            var command = new RelayCommand(
                o => this.CurrentWorkspace = new FlowFundsViewModel(),
                o => true);

            var commandViewModel = new CommandViewModel("Движение средств", "Resources/basket.png", command);

            return commandViewModel;
        }
        
        CommandViewModel CreateReportFunc()
        {
            var command = new RelayCommand(
                o => this.CurrentWorkspace = new ReportViewModel(),
                o => true);

            var commandViewModel = new CommandViewModel("Отчеты", "Resources/statistics.png", command);

            return commandViewModel;
        }

        CommandViewModel CreateItemsFunc()
        {
            var command = new RelayCommand(
                o => this.CurrentWorkspace = new ItemsViewModel(),
                o => true);

            var commandViewModel = new CommandViewModel("Категории и наименования", "Resources/category.png", command);

            return commandViewModel;
        }

        CommandViewModel CreateSettingsFunc()
        {
            var command = new RelayCommand(
                o => this.CurrentWorkspace = new SettingsViewModel(),
                o => true);

            var commandViewModel = new CommandViewModel("Настройки", "Resources/config.png", command);

            return commandViewModel;
        }

        #endregion // Private Helpers
    }
}